<?php

include_once ('bn_path.php');

class bn_user
{
    private $dl;
    private $gf;
    protected static $instance;

    function __construct()
    {
        $this->dl = new bn_dal();
        $this->gf = new bn_genralfunc;
    }


    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function insert($user, $pass, $rol, $mail, $nikname = '', $lang = 'eng')
    {
        if (!$this->gf->valid_mail($mail))
            return 2;

        if (self::has_email($mail))
            return 3;

        if (self::has_user($user))
            return 4;

        $pass = $this->gf->Hash_string($pass);
        if ($nikname == '')
            $nikname = $this->dl->getvalue('select name from rolname where id=?', array($rol));
        if ($rol == '')
            {
                $conf=bn_config::getInstance();
                $rol = $conf->get_value('default_rol');
            }
            
         $this->dl->insert("users", array($user,$pass,$rol,$nikname,$mail,$this->gf->date(),$lang));
        return 1;
    }

    public function delete($id)
    {
        if ($id > 1)
            $this->dl->delete('users', $id);
    }

    public function update($user, $pass, $rol, $nikname, $email, $lang, $id)
    {
        if ($user != '')
        {
            if (!self::has_user($user))
                $this->dl->update('users', array('user' => $user), array('id=' => $id));
        }

        if ($pass != '')
        {
            $pass = $this->gf->Hash_string($pass);
            $this->dl->update('users', array('pass' => $pass), array('id=' => $id));
        }

        if ($rol != '')
            $this->dl->update('users', array('rol' => $rol), array('id=' => $id));

        if ($nikname != '')
            $this->dl->update('users', array('nikname' => $nikname), array('id=' => $id));

        if ($lang != '')
            $this->dl->update('users', array('lang' => $lang), array('id=' => $id));

        if ($email != '')
        {
            if (!self::has_email($email))
                $this->dl->update('users', array('mail' => $email), array('id=' => $id));
        }
    }

    public function change_lang($lang, $user)
    {
        $this->dl->update('users', array('lang' => $lang), array('user=' => $user));
    }


    public function Select_all($col = '', $val = '')
    {
        if($col=='rol'&&!is_numeric($val))
        $val=$this->dl->getvalue('select id from rolname where name=?',$val);
        
        if (empty($col) || empty($val))
            return $this->dl->select('select users.id,user,(name)rol,nikname,mail,lang,date from users join rolname on rol=rolname.id'); 
        else
            return $this->dl->Select("select users.id,user,(name)rol,nikname,mail,lang,date from users join rolname on rol=rolname.id and users.$col=?",$val);
    }

    public function has_user($user)
    {
        return $this->dl->hasrow("select user from users where user=?", $user);
    }

    public function has_email($mail)
    {
        return $this->dl->hasrow("select user from users where mail=?", $mail);
    }

    public function forget_password($mail)
    {
        $row = $this->dl->Select('select id,mail from users where mail=?', array($mail))->
            fetch();
        if (isset($row))
        {
            $pass = $this->gf->RandomStrgenerate();
            $this->dl->update('users', array('pass' => $this->gf->Hash_string($pass)), array
                ('id=' => $id));
            $this->gf->mail->mail_sendHTML($row['mail'], 'forget password',
                "your new password is=" . $pass);
            return 1;
        } else
            return 5;
    }

    public function get_gravater($user, $default = '', $size = 120, $border = '')
    {
        $mail = $this->dl->getValue('select mail from users where user=?', $user);
        $mail = md5($mail);
        if (!empty($border))
            $border = "&border=$border";
        return "http://www.gravatar.com/avatar/$mail?default=http://www.mysite.com/null.jpg&size=$size$border";
    }


    function _destruct()
    {
        unset($this->dl);
    }
}

?>